<?php

abstract class Light_Controller_Admin extends Light_Controller
{
    /**
     * @var Light_Db_Table
     */
    protected $_model = null;

    /**
     * (non-PHPdoc)
     * @see Light_Controller::init()
     */
    public function init()
    {
        if (!$this->view->identity()) {
            $this->view->redirect(array('module' => 'users', 'controller' => 'index', 'action' => 'login'));
        }

        if (!$this->view->isAdmin()) {
            throw new Light_Exception('Access denied');
        }
    }

    /**
     * @return Light_Db_Table
     */
    public function getModel()
    {
        if ($this->_model === null) {
            $moduleName     = ucfirst($this->getModuleName());
            $controllerName = ucfirst($this->getControllerName());
            $modelName      = $moduleName . '_Model_' . $controllerName;
            $this->_model   = new $modelName();
        }

        return $this->_model;
    }

    /**
     * Index
     */
    public function indexAction()
    {
        $page = max((int) $this->getRequest()->getParam('page', 1), 1);

        $ipp  = (int) $this->getRequest()->getParam('ipp', 10);

        $paginator = new Light_Paginator($this->getModel());

        $paginator->setPageNumber($page)
                  ->setIpp($ipp);

        $this->view->assign('items', $paginator);
    }

    /**
     * Add item
     *
     * @param Light_Db_Table_Row $item
     */
    public function addAction(Light_Db_Table_Row $item = null)
    {
        $errors = array();

        $data = ($item !== null ? $item->toArray() : array());

        if ($this->getRequest()->isPost()) {

            $post = $this->getRequest()->getPost();

            $post = $this->_modifyPost($post);

            $data = array_merge($data, $post);

            $errors = $this->getModel()->validate($data, $item);

            if (empty($errors)) {
                if ($item === null) {
                    $item = $this->getModel()->createRow();
                }
                $item->setFromArray($data);
                $item->beforeSave();
                $item->save();
                $item->afterSave();
                $this->view->flashMessage()->setMessage();
                if (isset($data['apply'])) {
                    $this->view->redirect();
                } else {
                    $this->view->redirect(array(
                        'action' => 'index',
                        'id'     => null
                    ));
                }
            }
        }

        $this->view->assign('data',  $data);
        $this->view->assign('item',  $item);
        $this->view->assign('errors', $errors);
    }

    protected function _modifyPost($data)
    {
        return $data;
    }

    /**
     * Edit item
     */
    public function editAction()
    {
        $id = (int) $this->getRequest()->getParam('id');

        if (($item = $this->getModel()->find($id)->current()) === null) {
            $this->view->redirect(array(
                'action' => 'index',
                'id'     => null
            ));
        }

        $this->view->setViewScriptName('add');

        $this->addAction($item);
    }

    /**
     * Delete item
     */
    public function deleteAction()
    {
        $id = (int) $this->getRequest()->getParam('id');

        $item = $this->getModel()->find($id)->current();

        if ($item !== null) {
            $item->delete();
            $this->view->flashMessage()->setMessage();
        }

        $this->view->redirect(array(
            'action' => 'index',
            'id'     => null
        ));
    }
}